#include <math.h>
#include "Timer.h"


Timer::Timer()
{
    QueryPerformanceFrequency(&m_frequency);
    Reset();
}


void Timer::Reset()
{
    m_lastTick = GetTickCount64();
    QueryPerformanceCounter(&m_base);
    m_last = m_base;
}


// Get time elapsed in seconds since last call to Reset()
bool Timer::GetElapsedTime(double& elapsed)
{
    LARGE_INTEGER current;
    QueryPerformanceCounter(&current);
    ULONGLONG currentTick = GetTickCount64();

    elapsed = (double)(current.QuadPart-m_base.QuadPart) / (double)m_frequency.QuadPart;

    double dt = (double)(current.QuadPart-m_last.QuadPart) / (double)m_frequency.QuadPart;
    double dtTick = (double)(currentTick-m_lastTick) / 1000.0;
    m_last = current;
    m_lastTick = currentTick;

    return (abs(dt-dtTick) <= 0.5);
}
